# -*- coding:utf-8 -*-

from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from tsuchinoco.knowlege.models import Content,Comment,ContentForm,CommentForm

def content_index(request):
    """
    タスクの一覧を表示する
    """
    if request.method == 'GET':
        if request.session.get('user_id',None) == None:
            return HttpResponseRedirect('/')
        contents = Content.objects.all()
        print Content.objects.all().count()
        return render_to_response('contentindex.html',
                                  context_instance=RequestContext(request,
                                                                  {'user_id':request.session.get('user_id',
                                                                                                 None),
                                                                   'content_list':contents}))
    elif request.method == 'POST':
        pass

def new_content(request):
    """
    """
    if request.method == 'GET':
        form = ContentForm()
        print form
        return render_to_response('contentedit.html',
                                  context_instance=RequestContext(request,
                                                                  {'form':form,
                                                                   'error_message':''}))
    elif request.method == 'POST':
        content = Content()
        content.author = request.session.get('user_id',None)
        form = ContentForm(request.POST, instance=content)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/newwiki/')        
        else:
            print form.errors
            return render_to_response('contentedit.html',
                                      context_instance=RequestContext(request,
                                                                      {'form':form,
                                                                       'error_message':form.errors}))
def content_edit(request,user_id,content_id):
    """
    """
    if request.method == 'GET':
        if int(user_id) == request.session.get('user_id',0):
            content = Content.objects.get(pk=int(content_id))
            form = ContentForm(None,instance=content)
            return render_to_response('contentview.html',
                                      context_instance=RequestContext(request,
                                                                      {'form':form,
                                                                       'content_id':content_id,
                                                                       'user_id':user_id,
                                                                       'error_message':''}))
        else:
           pass
    elif request.method == 'POST':
        task = Tasks.objects.get(pk=int(task_id))
        form = TasksForm(request.POST, instance=task)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/task/')
        else:
            print form.errors
            return render_to_response('taskview.html',
                                      context_instance=RequestContext(request,
                                                                      {'form':form,
                                                                       'content_id':content_id,
                                                                       'user_id':user_id,
                                                                       'error_message':form.errors}))

def comment_post(request, content_id):
    if request.method == 'GET':
      pass
    elif request.method == 'POST':
      pass 

